#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_pub27.h>
#include <api/libpub29/lib_as.h>
#endif

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_signalstatus.h"

#include "control.h"

#include "mobile_input.h"

#include "win_rename.h"

#define DEB_PRINT	soc_printf

/*******************************************************************************
*	Objects definition
*******************************************************************************/

extern CONTAINER g_win_chan_rename;

extern CONTAINER chanrn_item1;	/* Name */

extern TEXT_FIELD chanrn_title;
extern TEXT_FIELD chanrn_txt1;
extern TEXT_FIELD chanrn_btntxt_ok;
extern TEXT_FIELD chanrn_btntxt_cancel;
extern TEXT_FIELD chanrn_btntxt_caps;
extern TEXT_FIELD chanrn_btntxt_del;

extern BITMAP  chanrn_btnbmp_caps;
extern BITMAP  chanrn_btnbmp_del;
extern BITMAP  chanrn_btnbmp_ok;
extern BITMAP  chanrn_btnbmp_cancel;
extern BITMAP  chanrn_btnbmp_abc;

extern EDIT_FIELD chanrn_edt1;	/* Name */

extern BITMAP     name_editflag_bmp;
extern TEXT_FIELD name_editfla_txt;

static VACTION chanrn_item_edt_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT chanrn_item_edt_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION chanrn_item_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT chanrn_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION chanrn_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT chanrn_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define WIN_SH_IDX	  WSTL_POP_WIN_01_HD
#define WIN_HL_IDX	  WSTL_POP_WIN_01_HD
#define WIN_SL_IDX	  WSTL_POP_WIN_01_HD
#define WIN_GRY_IDX  WSTL_POP_WIN_01_HD

#define CON_SH_IDX   WSTL_NOSHOW_IDX
#define CON_HL_IDX   WSTL_NOSHOW_IDX
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD

#define TXT_SH_IDX   WSTL_TEXT_04_HD
#define TXT_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXT_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_GRY_IDX  WSTL_BUTTON_07_HD

#define TITLE_SH_IDX    WSTL_POP_TXT_SH_HD
#define TITLE_HL_IDX    TITLE_SH_IDX
#define TITLE_SL_IDX    TITLE_SH_IDX
#define TITLE_GRY_IDX   TITLE_SH_IDX

#define EDT_SH_IDX   WSTL_TEXT_04_HD
#define EDT_HL_IDX   WSTL_BUTTON_04_HD
#define EDT_NAME_HL_IDX   WSTL_KEYBOARD_01_HD//WSTL_BUTTON_09
#define EDT_SL_IDX   WSTL_BUTTON_01_FG_HD
#define EDT_GRY_IDX  WSTL_BUTTON_07_HD

#define SEL_SH_IDX   WSTL_TEXT_04_HD
#define SEL_HL_IDX   WSTL_BUTTON_04_HD
#define SEL_SL_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_GRY_IDX  WSTL_BUTTON_07_HD

#define POPUPSH_IDX	WSTL_BUTTON_POP_SH_HD
#define POPUPHL_IDX	WSTL_BUTTON_POP_HL_HD

#ifndef SD_UI
#define	W_L     244 //380//120
#define	W_T     238 //278//160
#define	W_W     542//380
//#define	W_H     238//160

//Gavim@rename_abc
#define	W_H     300//160


#define TITLE_L (W_L + 10)
#define TITLE_T	 (W_T + 20)
#define TITLE_W (W_W - 20)
#define TITLE_H 40

#define CON_L	(W_L + 4)
#define CON_T	(TITLE_T +  TITLE_H + 20)
#define CON_W	(W_W - 8)
#define CON_H	48
#define CON_GAP	0

#define TXT_L_OF  	10
#define TXT_W  		114
#define TXT_H		48
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  (( W_W - SEL_W)/2) //(TXT_L_OF + TXT_W)
#define SEL_W  		456//320
#define SEL_H		48
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define BTN_L_OF	((W_W - BTN_W*2 - BTN_GAP)/2)
#define BTN_T_OF	80
#define BTN_W		80
#define BTN_H		30
#define BTN_GAP    4

#define COLBTN_L	(W_L + 40)
#define COLBTN_T	(W_T + W_H - 82)

#define CAPSFLG_L	(W_L + W_W - 150)
#define CAPSFLG_T	(TITLE_T + 20)
#else
#define	W_L     120 //380//120
#define	W_T     160 //278//160
#define	W_W     380//380
#define	W_H     180//160


#define TITLE_L (W_L + 10)
#define TITLE_T	 (W_T + 10)
#define TITLE_W (W_W - 20)
#define TITLE_H 30

#define CON_L	(W_L + 4)
#define CON_T	(TITLE_T +  TITLE_H + 10)
#define CON_W	(W_W - 8)
#define CON_H	40
#define CON_GAP	0

#define TXT_L_OF  	10
#define TXT_W  		80
#define TXT_H		40
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  (( W_W - SEL_W)/2) //(TXT_L_OF + TXT_W)
#define SEL_W  		300//320
#define SEL_H		40
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define BTN_L_OF	((W_W - BTN_W*2 - BTN_GAP)/2+5)
#define BTN_T_OF	50
#define BTN_W		24
#define BTN_H		24
#define BTN_GAP    2

#define COLBTN_L	(W_L + 15)
#define COLBTN_T	(W_T + W_H - 60)

#define CAPSFLG_L	(W_L + W_W - 130)
#define CAPSFLG_T	(TITLE_T + 12)

#endif

#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,hl,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,hl,CON_SL_IDX,CON_GRY_IDX,   \
    chanrn_item_keymap,chanrn_item_callback,  \
    conobj, ID,1)
    
#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_TITLE(root,varTxt,nxtObj,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TITLE_SH_IDX,TITLE_HL_IDX,TITLE_SL_IDX,TITLE_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,resID,NULL)

#define LDEF_TXT_BTN(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, POPUPSH_IDX,POPUPHL_IDX,POPUPSH_IDX,POPUPSH_IDX,   \
    chanrn_btn_keymap,chanrn_btn_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,resID,NULL)


#define LDEF_EDIT(root, varNum, nxtObj, ID, l, t, w, h, hl,align,style,cursormode,pat,sub,str)	\
    DEF_EDITFIELD(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, EDT_SH_IDX,hl,EDT_SL_IDX,EDT_GRY_IDX,   \
    chanrn_item_edt_keymap,chanrn_item_edt_callback,  \
    align, 4,0,style,pat,17,cursormode,NULL,sub,str)

#define LDEF_MENU_ITEM_EDT(root,varCon,nxtObj,varTxt,varNum,ID,IDu,IDd,\
						l,t,w,h,edthl,resID,align,style,curMode,pat,sub,str) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,CON_HL_IDX,&varNum,ID)   \
    LDEF_EDIT(&varCon,varNum,NULL   ,ID, l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,edthl,align,style,curMode,pat,sub,str)

 //   LDEF_TXT(&varCon,varTxt,&varNum,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID)    
 #ifndef SD_UI
#define LDEF_BMP_BTN(root,varBmp,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,iconID) \
	DEF_BITMAP(varBmp, root, nxtObj, C_ATTR_ACTIVE,0, 	\
	ID,IDl,IDr,IDu,IDd, l, t, 28, 40, WSTL_TEXT_04_8BIT, WSTL_TEXT_04_8BIT, WSTL_TEXT_04_8BIT, WSTL_TEXT_04_8BIT,	\
	NULL, NULL,	\
	 C_ALIGN_LEFT | C_ALIGN_VCENTER, 0, 0, iconID)
#else
#define LDEF_BMP_BTN(root,varBmp,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,iconID) \
	DEF_BITMAP(varBmp, root, nxtObj, C_ATTR_ACTIVE,0, 	\
	ID,IDl,IDr,IDu,IDd, l, t, 24, 24, WSTL_TEXT_04_8BIT, WSTL_TEXT_04_8BIT, WSTL_TEXT_04_8BIT, WSTL_TEXT_04_8BIT,	\
	NULL, NULL,	\
	 C_ALIGN_LEFT | C_ALIGN_VCENTER, 0, 0, iconID)
#endif

#define LDEF_TXT_BTN_EXT(root,varTxt,nxtObj,l,t,w,h,str)	\
	DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0,str)


#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    chanrn_keymap,chanrn_callback,  \
    nxtObj, focusID,0)

/**************************************************************************/
/*
 DEF_BITMAP(name_editflag_bmp,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, 0,0,24,24, WSTL_TEXT_04,WSTL_TEXT_04,WSTL_TEXT_04,WSTL_TEXT_04,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0)
*/
#ifndef SD_UI
//Gavim@rename_abc
#define AOV_ABC_L 290
#define AOV_ABC_T 380
#define AOV_ABC_W 451
#define AOV_ABC_H 60

DEF_BITMAP(chanrn_btnbmp_abc,NULL,NULL,C_ATTR_ACTIVE,0, \
    0, 0, 0, 0, 0, AOV_ABC_L,AOV_ABC_T,AOV_ABC_W,AOV_ABC_H ,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,NULL)
    
 DEF_BITMAP(name_editflag_bmp,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, 0,0,28,40, WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0)

UINT16 name_editflg_str[30];
/* 
DEF_TEXTFIELD(name_editfla_txt,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, 0,0,70,24, WSTL_TEXT_04,WSTL_TEXT_04,WSTL_TEXT_04,WSTL_TEXT_04,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0,name_editflg_str)
*/
DEF_TEXTFIELD(name_editfla_txt,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, 0,0,70,40, WSTL_POP_TXT_SH_01_HD,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0,name_editflg_str)
 #else 
 DEF_BITMAP(name_editflag_bmp,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, 0,0,28,24, WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0)

UINT16 name_editflg_str[30];

DEF_TEXTFIELD(name_editfla_txt,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, 0,0,40,24, WSTL_POP_TXT_SH_01_HD,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0,name_editflg_str)
 #endif

/**************************************************************************/



LDEF_TITLE(g_win_chan_rename, chanrn_title, &chanrn_item1, \
		TITLE_L, TITLE_T, TITLE_W, TITLE_H, 0)

//extern char name_pat[];
char name_pat[]  = "s17";

extern UINT16 chan_name_str[];

LDEF_MENU_ITEM_EDT(g_win_chan_rename,chanrn_item1,&chanrn_btnbmp_caps,chanrn_txt1,chanrn_edt1, 1, 1, 1, \
		CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, EDT_NAME_HL_IDX,RS_INFO_NAME,  C_ALIGN_LEFT | C_ALIGN_VCENTER, NORMAL_EDIT_MODE, CURSOR_NORMAL, name_pat,NULL, chan_name_str)
#ifndef SD_UI
LDEF_BMP_BTN(&g_win_chan_rename,chanrn_btnbmp_caps,&chanrn_btntxt_caps,0,0,0,0,0, \
		COLBTN_L, COLBTN_T,IM_EPG_COLORBUTTON_RED)
LDEF_TXT_BTN_EXT(&g_win_chan_rename, chanrn_btntxt_caps, &chanrn_btnbmp_del, \
		COLBTN_L+28*1, COLBTN_T, 100, 40, display_strs[0])
LDEF_BMP_BTN(&g_win_chan_rename,chanrn_btnbmp_del,&chanrn_btntxt_del,0,0,0,0,0, \
		COLBTN_L+100+28*1, COLBTN_T,IM_EPG_COLORBUTTON_GREEN)
LDEF_TXT_BTN_EXT(&g_win_chan_rename, chanrn_btntxt_del, &chanrn_btnbmp_ok, \
		COLBTN_L+100+28*2, COLBTN_T, 70, 40, display_strs[1])		
LDEF_BMP_BTN(&g_win_chan_rename,chanrn_btnbmp_ok,&chanrn_btntxt_ok,0,0,0,0,0, \
		COLBTN_L+170+28*2, COLBTN_T,IM_EPG_COLORBUTTON_YELLOW)
LDEF_TXT_BTN_EXT(&g_win_chan_rename, chanrn_btntxt_ok, &chanrn_btnbmp_cancel, \
		COLBTN_L+170+28*3, COLBTN_T, 60, 40, display_strs[2])
LDEF_BMP_BTN(&g_win_chan_rename,chanrn_btnbmp_cancel,&chanrn_btntxt_cancel,0,0,0,0,0, \
		COLBTN_L+230+28*3, COLBTN_T,IM_EPG_COLORBUTTON_BLUE)
LDEF_TXT_BTN_EXT(&g_win_chan_rename, chanrn_btntxt_cancel, NULL, \
		COLBTN_L+230+28*4, COLBTN_T, 124, 40, display_strs[3])

/*
LDEF_TXT_BTN(g_win_chan_rename, chanrn_btntxt_ok,&chanrn_btntxt_cancel,2,3,3,1,1,	\
			CON_L + BTN_L_OF, CON_T + BTN_T_OF, BTN_W, BTN_H, RS_COMMON_SAVE)

LDEF_TXT_BTN(g_win_chan_rename, chanrn_btntxt_cancel, NULL            ,3,2,2,1,1,	\
			CON_L + BTN_L_OF + BTN_W + BTN_GAP, CON_T + BTN_T_OF, BTN_W, BTN_H, RS_COMMON_CANCEL)
*/
#else
LDEF_BMP_BTN(&g_win_chan_rename,chanrn_btnbmp_caps,&chanrn_btntxt_caps,0,0,0,0,0, \
		COLBTN_L, COLBTN_T,IM_EPG_COLORBUTTON_RED)
LDEF_TXT_BTN_EXT(&g_win_chan_rename, chanrn_btntxt_caps, &chanrn_btnbmp_del, \
		COLBTN_L+28*1, COLBTN_T, 50, 24, display_strs[0])
LDEF_BMP_BTN(&g_win_chan_rename,chanrn_btnbmp_del,&chanrn_btntxt_del,0,0,0,0,0, \
		COLBTN_L+60+28*1, COLBTN_T,IM_EPG_COLORBUTTON_GREEN)
LDEF_TXT_BTN_EXT(&g_win_chan_rename, chanrn_btntxt_del, &chanrn_btnbmp_ok, \
		COLBTN_L+60+28*2, COLBTN_T, 40, 24, display_strs[1])		
LDEF_BMP_BTN(&g_win_chan_rename,chanrn_btnbmp_ok,&chanrn_btntxt_ok,0,0,0,0,0, \
		COLBTN_L+110+28*2, COLBTN_T,IM_EPG_COLORBUTTON_YELLOW)
LDEF_TXT_BTN_EXT(&g_win_chan_rename, chanrn_btntxt_ok, &chanrn_btnbmp_cancel, \
		COLBTN_L+110+28*3, COLBTN_T, 30, 24, display_strs[2])
LDEF_BMP_BTN(&g_win_chan_rename,chanrn_btnbmp_cancel,&chanrn_btntxt_cancel,0,0,0,0,0, \
		COLBTN_L+155+28*3, COLBTN_T,IM_EPG_COLORBUTTON_BLUE)
LDEF_TXT_BTN_EXT(&g_win_chan_rename, chanrn_btntxt_cancel, NULL, \
		COLBTN_L+155+28*4, COLBTN_T, 80, 24, display_strs[3])
#endif
LDEF_WIN(g_win_chan_rename, &chanrn_title,W_L, W_T,W_W,W_H,1)


#define CHAN_NAME_ID	1
#define BTN_OK_ID		2
#define BTN_CANCEL_ID	3

/*******************************************************************************
*	Local vriable & function declare
*******************************************************************************/

//char name_pat[]  = "s17";

UINT16 chan_name_str[20];
UINT16 orbit_sub[10];

UINT32 edit_prog_id;
UINT8  edit_prog_ok; /* 0 - cancel : 1 save*/

check_name_func name_check_func;
	
INT32 win_chanrn_check_input(UINT16* name);


void draw_caps_flag(UINT16 x, UINT16 y, UINT32 mode); /*  mode : 0 -hide, 1 -draw*/
void draw_caps_del_colbtn(UINT16 x, UINT16 y, UINT32 mode);
void draw_ok_cancel_colbtn(UINT16 x, UINT16 y, UINT32 mode);


static void win_chanrn_draw_color_btns(void);

UINT16 btn_bmp_ids_truecolor[][4] = 
{    
    {
        IM_EPG_COLORBUTTON_RED,
        IM_EPG_COLORBUTTON_GREEN,
        IM_EPG_COLORBUTTON_YELLOW,
        IM_EPG_COLORBUTTON_BLUE
	}
    ,
	{
        IM_EPG_COLORBUTTON_RED,
	    IM_EPG_COLORBUTTON_GREEN,
	    IM_EPG_COLORBUTTON_YELLOW,
	    IM_EPG_COLORBUTTON_BLUE
    }
};


char* btn_txt_strs[] = 
{
	"CAPS", "DEL", "OK", "CANCEL"
};
#ifndef SD_UI
UINT8 btn_txt_widths[] =
{
	100, 70, 60, 124
};
#else
UINT8 btn_txt_widths[] =
{
	80, 50, 40, 80
};
#endif


static void set_help_content(BOOL param)
{
	if(param)
	{
		OSD_SetTextFieldContent(&chanrn_btntxt_caps,STRING_ANSI,(UINT32)btn_txt_strs[0]);
		OSD_SetTextFieldContent(&chanrn_btntxt_del,STRING_ANSI,(UINT32)btn_txt_strs[1]);
		OSD_SetTextFieldContent(&chanrn_btntxt_ok,STRING_ANSI,(UINT32)btn_txt_strs[2]);	
		OSD_SetTextFieldContent(&chanrn_btntxt_cancel,STRING_ANSI,(UINT32)btn_txt_strs[3]);
	}
	else
	{
		OSD_SetTextFieldContent(&chanrn_btntxt_caps,STRING_ANSI,(UINT32)"");
		OSD_SetTextFieldContent(&chanrn_btntxt_del,STRING_ANSI,(UINT32)"");
		OSD_SetTextFieldContent(&chanrn_btntxt_ok,STRING_ANSI,(UINT32)"");	
		OSD_SetTextFieldContent(&chanrn_btntxt_cancel,STRING_ANSI,(UINT32)"");
	}
}

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/

#define VACT_OK		(VACT_PASS + 1)
#define VACT_CANCEL	(VACT_PASS + 2)

static VACTION chanrn_item_edt_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	UINT8 bID;
	UINT8 bStyle;
	EDIT_FIELD	*edf;

	edf = (EDIT_FIELD*)pObj;

	bID = OSD_GetObjID(pObj);	

	bStyle = edf->bStyle;
	
	switch(key)
    {
	case V_KEY_0:	case V_KEY_1:	case V_KEY_2:	case V_KEY_3:
	case V_KEY_4:	case V_KEY_5:	case V_KEY_6:	case V_KEY_7:
	case V_KEY_8:	case V_KEY_9:
		act = key - V_KEY_0 + VACT_NUM_0;
		break;
	case V_KEY_LEFT:
	case V_KEY_RIGHT:
		act = (key == V_KEY_LEFT)? VACT_EDIT_LEFT : VACT_EDIT_RIGHT;
		break;
	case V_KEY_ENTER:
		act = VACT_EDIT_ENTER;
		break;
	case V_KEY_EXIT:
		if(bStyle&EDIT_STATUS)
			act = VACT_EDIT_SAVE_EXIT;
		else
			act = VACT_PASS;
		break;			
	case V_KEY_RED:
		act = VACT_CAPS;
		break;
	case V_KEY_GREEN:
		act = VACT_DEL;
		break;
	case V_KEY_BLUE:
	case V_KEY_MENU:
		act = VACT_CLOSE;
	default:
		act = VACT_PASS;
	}

	return act;	 
}

static PRESULT chanrn_item_edt_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8 bID;
	UINT8 unact;
	UINT8 caps;
	mobile_input_type_t mobile_input_type;

	caps = mobile_input_get_caps();

	mobile_input_type.type 		= MOBILE_INPUT_NORMAL;
	mobile_input_type.caps_flag = caps? MOBILE_CAPS_INIT_UP : MOBILE_CAPS_INIT_LOW;
	mobile_input_type.maxlen    = 17;
	mobile_input_type.fixlen_flag = 0;
	mobile_input_type.callback	= NULL;


	bID = OSD_GetObjID(pObj);

	switch(event)
	{
	case EVN_KEY_GOT:
		if(bID == CHAN_NAME_ID)
		{
			ret = mobile_input_proc((EDIT_FIELD*)pObj,(VACTION)(param1>>16), param1 & 0xFFFF, param2);
			if(ret == PROC_LOOP)
				draw_caps_flag(CAPSFLG_L,CAPSFLG_T, 1);
		}
		break;
	case EVN_PRE_CHANGE:
		break;
	case EVN_POST_CHANGE:
		break;
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16);
		break;
	case EVN_FOCUS_PRE_GET:
	case EVN_PARENT_FOCUS_PRE_GET:
		mobile_input_init((EDIT_FIELD*)pObj,&mobile_input_type);
		break;
	case EVN_FOCUS_PRE_LOSE:
	case EVN_PARENT_FOCUS_PRE_LOSE:		
		mobile_input_init((EDIT_FIELD*)pObj,&mobile_input_type);
		break;
	}

	return ret;
}

static VACTION chanrn_item_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;

	return act;	
}

static PRESULT chanrn_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;

	return ret;
}

static VACTION chanrn_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
	case V_KEY_YELLOW:
		act = VACT_OK;
		break;
	case V_KEY_BLUE:
		act = VACT_CANCEL;
		break;
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_CLOSE;
		break;
	default:
		act = VACT_PASS;
	}

	return act;	
}

static PRESULT chanrn_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	VACTION unact;
	UINT8 back_saved;
	mobile_input_type_t mobile_input_type;

	mobile_input_type.type 		= MOBILE_INPUT_NORMAL;
	mobile_input_type.caps_flag = MOBILE_CAPS_INIT_LOW;
	mobile_input_type.maxlen    = 17;
	mobile_input_type.fixlen_flag = 0;
	mobile_input_type.callback	= NULL;

	switch(event)
	{
		case EVN_PRE_OPEN:
			mobile_input_init(&chanrn_edt1,&mobile_input_type);
			break;
		case EVN_POST_OPEN:
			draw_caps_flag(CAPSFLG_L,CAPSFLG_T, 1);
		//	draw_caps_del_colbtn(COLBTN_L, COLBTN_T,1);
		// 	draw_ok_cancel_colbtn(COLBTN_L + btn_txt_widths[0] + btn_txt_widths[1] + 24*2, COLBTN_T,1);

			break;
		case EVN_PRE_CLOSE:
			/* Make OSD not flickering */
			*((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;			
			break;
		case EVN_POST_CLOSE:
			break;
		case EVN_UNKNOWN_ACTION:
			unact = (VACTION)(param1>>16);
			if(unact == VACT_OK)
			{
				if(ComUniStrLen(chan_name_str) == 0)
				{
					win_compopup_init(WIN_POPUP_TYPE_OK);
					win_compopup_set_msg(NULL,NULL, RS_MSG_NAME_EMPTY);
					win_compopup_open_ext(&back_saved);	
					break;
				}
				
				if(name_check_func != NULL)
				{
					if(name_check_func(chan_name_str) == 0)
					{
						edit_prog_ok = 1;
						ret = PROC_LEAVE;				
					}
				}
				else
				{
					edit_prog_ok = 1;
					ret = PROC_LEAVE;
				}
			}
			else if(unact == VACT_CANCEL)
				ret = PROC_LEAVE;
	}
	
	return ret;
}


void draw_caps_flag(UINT16 x, UINT16 y, UINT32 mode)
{
	BITMAP		*bmp;
	TEXT_FIELD	*txt;
	//Gavim@rename_abc
	BITMAP *bmp2 = &chanrn_btnbmp_abc;
	

	UINT16 l,t;

	l = x;
	t = y;
		
    bmp = &name_editflag_bmp;
	txt = & name_editfla_txt;
	bmp->head.frame.uTop  = t;
	txt->head.frame.uTop  = t;

	/* CAPS flag */
	bmp->head.frame.uLeft = l;

    OSD_SetColor(bmp,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT);
    OSD_SetBitmapContent(bmp, mode==1? IM_RENAME_PENCIL: 0);
    OSD_SetColor(txt,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT);
    OSD_DrawObject((POBJECT_HEAD)bmp, C_UPDATE_ALL);
#ifndef SD_UI
	txt->head.frame.uWidth = btn_txt_widths[0];
#else
	txt->head.frame.uWidth = 40;
#endif
	txt->head.frame.uLeft = l + bmp->head.frame.uWidth;
	if(mobile_input_get_caps() == 0)
		OSD_SetTextFieldContent(txt, STRING_ANSI,(UINT32)"abc");
	else
		OSD_SetTextFieldContent(txt, STRING_ANSI,(UINT32)"ABC");
	if(mode == 0)
		OSD_SetTextFieldContent(txt, STRING_ANSI,(UINT32)"");
	OSD_DrawObject((POBJECT_HEAD)txt, C_UPDATE_ALL);
	
	//----------------------------------------
	libc_printf("show bmp----\n");
	OSD_SetBitmapContent(bmp2, IM_ABC);
	OSD_DrawObject((POBJECT_HEAD)bmp2, C_UPDATE_ALL);
	//-----------------------------------------  	
}


static void draw_btn(UINT16 l, UINT16 t, UINT32 mode, UINT32 idx)
{
	BITMAP		*bmp;
	TEXT_FIELD	*txt;

    bmp = &name_editflag_bmp;
	txt = &name_editfla_txt;
	bmp->head.frame.uTop  = t;
	txt->head.frame.uTop  = t;

	bmp->head.frame.uLeft = l;
    
    OSD_SetColor(bmp,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT);
    OSD_SetBitmapContent(bmp, mode == 1? btn_bmp_ids_truecolor[0][idx] : 0);
    OSD_SetColor(txt,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT);
    OSD_DrawObject((POBJECT_HEAD)bmp, C_UPDATE_ALL);

	txt->head.frame.uWidth = btn_txt_widths[idx];
	txt->head.frame.uLeft = l + bmp->head.frame.uWidth;
	OSD_SetTextFieldContent(txt, STRING_ANSI,mode == 1? (UINT32)btn_txt_strs[idx] : (UINT32)"");
	OSD_DrawObject((POBJECT_HEAD)txt, C_UPDATE_ALL);


}

static void draw_btn_ext(UINT16 l, UINT16 t, UINT32 mode,UINT16 bmpid, UINT32 txtidx )
{
	BITMAP		*bmp;
	TEXT_FIELD	*txt;

    bmp = &name_editflag_bmp;
	txt = &name_editfla_txt;
	bmp->head.frame.uTop  = t;
	txt->head.frame.uTop  = t;

	bmp->head.frame.uLeft = l;
    //OSD_SetColor(bmp,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT);
    OSD_SetColor(bmp,WSTL_NOSHOW_IDX,WSTL_NOSHOW_IDX,WSTL_NOSHOW_IDX,WSTL_NOSHOW_IDX);
    //OSD_SetColor(txt,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT,WSTL_TEXT_04_8BIT);
    OSD_SetColor(txt,WSTL_POP_TEXT_8BIT,WSTL_POP_TEXT_8BIT,WSTL_POP_TEXT_8BIT,WSTL_POP_TEXT_8BIT);
	OSD_SetBitmapContent(bmp, (mode == 1)? bmpid : 0);	
	OSD_DrawObject((POBJECT_HEAD)bmp, C_UPDATE_ALL);

	txt->head.frame.uWidth = btn_txt_widths[txtidx];
	txt->head.frame.uLeft = l + bmp->head.frame.uWidth;
	OSD_SetTextFieldContent(txt, STRING_ANSI,(mode == 1)? (UINT32)btn_txt_strs[txtidx] : 0);
	OSD_DrawObject((POBJECT_HEAD)txt, C_UPDATE_ALL);


}

void draw_caps_del_colbtn(UINT16 x, UINT16 y, UINT32 mode)
{
	draw_btn(x,y,mode, 0);
	draw_btn(x + btn_txt_widths[0] + 14,y,mode, 1);
}


void draw_ok_cancel_colbtn(UINT16 x, UINT16 y, UINT32 mode)
{
	draw_btn(x,y,mode, 2);
	draw_btn(x + btn_txt_widths[2] + 14,y,mode, 3);
}

void draw_caps_colbtn(UINT16 x, UINT16 y, UINT32 mode)
{
	draw_btn(x,y,mode, 0);
}

void draw_del_colbtn(UINT16 x, UINT16 y, UINT32 mode)
{
	draw_btn(x,y,mode, 1);
}


void draw_ok_colbtn(UINT16 x, UINT16 y, UINT32 mode)
{
	draw_btn(x,y,mode, 2);
}

void draw_cancel_colbtn(UINT16 x, UINT16 y, UINT32 mode)
{
	draw_btn(x,y,mode, 3);
}

void draw_caps_colbtn_ext(UINT16 x, UINT16 y, UINT32 mode,UINT16 bmpidx)
{
    draw_btn_ext(x,y,mode,bmpidx,0);
}

void draw_del_colbtn_ext(UINT16 x, UINT16 y, UINT32 mode,UINT16 bmpidx)
{
    draw_btn_ext(x,y,mode,bmpidx,1);
}

void draw_ok_colbtn_ext(UINT16 x, UINT16 y, UINT32 mode,UINT16 bmpidx)
{
    draw_btn_ext(x,y,mode,bmpidx,2);
}

void draw_cancel_colbtn_ext(UINT16 x, UINT16 y, UINT32 mode,UINT16 bmpidx)
{
    draw_btn_ext(x,y,mode,bmpidx,3);
}


INT32 win_chanrn_check_input(UINT16* name)
{
	EDIT_FIELD	*edf;
	UINT32 cnt,i;
	P_NODE p_node;
	UINT8 back_saved;
	UINT8 av_flag;


	get_prog_by_id(edit_prog_id, &p_node);

	if(DB_STRCMP(name, (UINT16*)p_node.service_name) == 0 )
		return 0;

CHECK_DUPLICATE:
	av_flag = sys_data_get_cur_chan_mode();
	cnt = get_prog_num(VIEW_ALL | av_flag, 0);
	
	for(i=0;i<cnt;i++)
	{
		get_prog_at(i,&p_node);
		
		if(p_node.prog_id == edit_prog_id)
			continue;
		if(DB_STRCMP(name, (UINT16*)p_node.service_name) == 0)
		{
			win_compopup_init(WIN_POPUP_TYPE_OK);
			win_compopup_set_msg(NULL,NULL, RS_DISPLAY_NAME_EXIST);
			win_compopup_open_ext(&back_saved);
			return -1;
		}
	}

	return 0;
}


UINT32 win_chanrename_open(UINT32 prog_id,UINT8** name)
{
	P_NODE p_node;

	
	edit_prog_id = prog_id;
	get_prog_by_id(prog_id,&p_node);	

	return win_rename_open((UINT16*)p_node.service_name, name,win_chanrn_check_input);

}

UINT32 win_recrename_open(UINT16* prog_name,UINT8** name)
{
	
	return win_rename_open(prog_name, name,NULL);

}


UINT32 win_rename_open(UINT16* set_name,UINT8** ret_name,check_name_func check_func )
{
	CONTAINER	*win;
	PRESULT ret = PROC_LOOP;
	UINT32 hkey;
	TEXT_FIELD	*txt;
	EDIT_FIELD	*eft;
	UINT16 strID;

	win = &g_win_chan_rename;
	wincom_backup_region(&win->head.frame);
    BOOL old_value = ap_enable_key_task_get_key(TRUE);
	edit_prog_ok = 0;
	name_check_func = check_func;

	eft = &chanrn_edt1;
	MEMSET(chan_name_str,0,sizeof(chan_name_str));
	OSD_SetEditFieldContent(eft, STRING_UNICODE, (UINT32)set_name);	

 	txt = &chanrn_title;
	strID = RS_HELP_RENAME;
	OSD_SetTextFieldContent(txt, STRING_ID,(UINT32)strID);

	set_help_content(TRUE);

	OSD_SetContainerFocus(win, CHAN_NAME_ID);

	OSD_ObjOpen((POBJECT_HEAD)win, 0);
	
	while(ret != PROC_LEAVE)
	{
		hkey = ap_get_key_msg();
		if(hkey == INVALID_HK || hkey == INVALID_MSG)
		    continue;

		ret = OSD_ObjProc((POBJECT_HEAD)win, (MSG_TYPE_KEY<<16),hkey, 0);
	}
	

	*ret_name = (UINT8*)chan_name_str;
	wincom_restore_region();
    ap_enable_key_task_get_key(old_value);

	return edit_prog_ok;
}

UINT32 win_makefolder_open(UINT16* set_name,UINT8** ret_name,check_name_func check_func )
{
	CONTAINER	*win;
	PRESULT ret = PROC_LOOP;
	UINT32 hkey;
	TEXT_FIELD	*txt;
	EDIT_FIELD	*eft;
	UINT16 strID;

	win = &g_win_chan_rename;
	wincom_backup_region(&win->head.frame);
    BOOL old_value = ap_enable_key_task_get_key(TRUE);
	edit_prog_ok = 0;
	name_check_func = check_func;

	eft = &chanrn_edt1;
	MEMSET(chan_name_str,0,sizeof(chan_name_str));
	OSD_SetEditFieldContent(eft, STRING_UNICODE, (UINT32)set_name);	

 	txt = &chanrn_title;
	strID = RS_HELP_NEW_FOLDER;
	OSD_SetTextFieldContent(txt, STRING_ID,(UINT32)strID);

	OSD_SetContainerFocus(win, CHAN_NAME_ID);
	set_help_content(TRUE);

	OSD_ObjOpen((POBJECT_HEAD)win, 0);
	
	while(ret != PROC_LEAVE)
	{
		hkey = ap_get_key_msg();
		if(hkey == INVALID_HK || hkey == INVALID_MSG)
		    continue;

		ret = OSD_ObjProc((POBJECT_HEAD)win, (MSG_TYPE_KEY<<16),hkey, 0);
	}
	

	*ret_name = (UINT8*)chan_name_str;
	wincom_restore_region();
    ap_enable_key_task_get_key(old_value);

	return edit_prog_ok;
}

